package com.heba.popupwindow.callback;

import android.content.Context;

import com.heba.bean.ShareObject;
import com.heba.greenmulu.R;
import com.heba.helper.SocialShareHelper;
import com.heba.utils.CommonUtils;
import com.heba.utils.ToastShow;

import java.util.HashMap;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.whatsapp.WhatsApp;

/**
 * 分享
 * Created by jason on 2020/3/16.
 */
public class ShareListener implements ShareBaseListener {

    private Context context;
    private ShareObject shareObject;  //需要分享的实体类

    private MyPlatformActionListener listener;

    public ShareListener(Context context) {
        this.context = context;
        listener = new MyPlatformActionListener();
    }

    @Override
    public void shareFacebook() {
        if (context == null)
            return;
        if (!SocialShareHelper.isApkExist(context, SocialShareHelper.packageFacebook)) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_facebook_not_exist));
            return;
        }

        if (null != shareObject)
            shareFacebookWebPager(shareObject);

        //調用系統分享到Facebook
//        Intent facebookIntent = new Intent(Intent.ACTION_SEND);
//        facebookIntent.setPackage(SocialShareHelper.packageFacebook);
//        facebookIntent.setType("text/plain");
//        facebookIntent.putExtra(Intent.EXTRA_TEXT, shareContent.isEmpty() ? "分享到Facebook" : shareContent);
//        context.startActivity(facebookIntent);
    }

    @Override
    public void shareWhatsApp() {
        if (context == null)
            return;
        if (!SocialShareHelper.isApkExist(context, SocialShareHelper.packageWhatsApp)) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_whatsapp_not_exist));
            return;
        }

        if (null != shareObject)
            shareWhatsAppText(shareObject);

        //調用系統分享到WhatsApp
//        if (null != shareObject) {
//            Intent wechatIntent = new Intent(Intent.ACTION_SEND);
//            wechatIntent.setPackage(SocialShareHelper.packageWhatsApp);
//            wechatIntent.setType("text/plain");
//            wechatIntent.putExtra(Intent.EXTRA_TEXT, shareObject.getShareContent() + " " + shareObject.getShareLink());
//            context.startActivity(wechatIntent);
//        }
    }

    @Override
    public void shareWechat() {
        if (context == null)
            return;
        if (!SocialShareHelper.isApkExist(context, SocialShareHelper.packageWeixin)) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_wechat_not_exist));
            return;
        }
        if (null != shareObject)
            shareWechatWebpage(shareObject);

        //調用系統分享到微信聯繫人
//        Intent wechatIntent = new Intent(Intent.ACTION_SEND);
//        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
//        wechatIntent.setComponent(comp);
////        wechatIntent.setPackage("com.tencent.mm.ui.tools.ShareImgUI");
//        wechatIntent.setType("text/plain");
//        wechatIntent.putExtra(Intent.EXTRA_TEXT, shareContent.isEmpty() ? "分享到微信" : shareContent);
//        context.startActivity(wechatIntent);
    }

    @Override
    public void shareCircle() {
        if (context == null)
            return;
        if (!SocialShareHelper.isApkExist(context, SocialShareHelper.packageWeixin)) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_wechat_not_exist));
            return;
        }

        if (null != shareObject)
            shareWechatCircleWebpage(shareObject);

        //調用系統分享到微信朋友圈
//        Intent wechatIntent = new Intent(Intent.ACTION_SEND);
//        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
//        wechatIntent.setComponent(comp);
////        wechatIntent.setPackage("com.tencent.mm.ui.tools.ShareImgUI");
//        wechatIntent.setType("text/plain");
//        wechatIntent.putExtra(Intent.EXTRA_TEXT, shareContent.isEmpty() ? "分享到朋友圈" : shareContent);
//        context.startActivity(wechatIntent);
    }

    @Override
    public void shareSMS() {

    }

    @Override
    public void deleteDiary() {

    }

    @Override
    public void goTutorial() {

    }

    @Override
    public void hiddenDiary() {

    }

    @Override
    public void removeTag() {

    }

    @Override
    public void blockUser(){

    }

    @Override
    public void sendProduct() {

    }

    @Override
    public void collectProduct() {

    }

    @Override
    public void updateProduct() {

    }

    @Override
    public void collectDiary() {

    }

    @Override
    public void reportDiary() {

    }

    @Override
    public void sendUser() {

    }

    @Override
    public void reportUser() {

    }

    /**
     * 分享实体类
     *
     * @param shareObject
     */
    public void setShareObject(ShareObject shareObject) {
        this.shareObject = shareObject;
    }

    /**
     * 分享到facebook
     *
     * @param object
     */
    private void shareFacebookWebPager(ShareObject object) {
        Platform platform = ShareSDK.getPlatform(Facebook.NAME);
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setUrl(object.getShareLink());
        shareParams.setHashtag(object.getShareTitle());
        shareParams.setQuote(CommonUtils.getDecodeText(object.getShareContent()));
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(listener);
        platform.share(shareParams);
    }

    /**
     * 分享到whatsapp
     *
     * @param object
     */
    private void shareWhatsAppText(ShareObject object) {
        Platform platform = ShareSDK.getPlatform(WhatsApp.NAME);
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setText(CommonUtils.getDecodeText(object.getShareContent()) + "\n" + object.getShareLink());
        platform.setPlatformActionListener(listener);
        platform.share(shareParams);
    }

    /**
     * 分享到微信
     *
     * @param object
     */
    private void shareWechatWebpage(ShareObject object) {
        Platform platform = ShareSDK.getPlatform(Wechat.NAME);
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(object.getShareTitle());
        shareParams.setText(CommonUtils.getDecodeText(object.getShareContent()));
        shareParams.setUrl(object.getShareLink());
        shareParams.setImageUrl(object.getShareImageUrl());
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(listener);
        platform.share(shareParams);
    }

    /**
     * 分享到朋友圈
     *
     * @param object
     */
    private void shareWechatCircleWebpage(ShareObject object) {
        Platform platform = ShareSDK.getPlatform(WechatMoments.NAME);
        Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(CommonUtils.getDecodeText(object.getShareContent()));
        shareParams.setText(CommonUtils.getDecodeText(object.getShareContent()));
        shareParams.setUrl(object.getShareLink());
        shareParams.setImageUrl(object.getShareImageUrl());
        shareParams.setShareType(Platform.SHARE_WEBPAGE);
        platform.setPlatformActionListener(listener);
        platform.share(shareParams);
    }

    /**
     * 分享回调
     */
    class MyPlatformActionListener implements PlatformActionListener {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_complete));
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_error));
        }

        @Override
        public void onCancel(Platform platform, int i) {
            ToastShow.displayToast(context, context.getResources().getString(R.string.share_cancel));
        }
    }

}
